package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.common.enums.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.dto.activity.ActivityDTO;
import com.qk.entity.Activity;
import com.qk.management.mapper.ActivityMapper;
import com.qk.management.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Just、倾城
 * @created 2025-10-12
 * @description
 *
 */
@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;

    @Override
    public List<Activity> listByType(String type) {
        // 参数校验 无
        return activityMapper.listByType(type);
    }

    @Override
    public void update(Activity activity) {
        // 参数校验 无
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.update(activity);
    }


    @Override
    public Activity selectById() {
        // 参数校验 无

        return activityMapper.selectById();
    }

    @Override
    public void add(Activity activity) {
        // 参数校验无
        activity.setCreateTime(LocalDateTime.now());
        activity.setUpdateTime(LocalDateTime.now());
        activityMapper.add(activity);

    }



    @Override
    public void delete(Integer id) {
        // 参数校验
        if(BeanUtil.isEmpty( id)){
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        activityMapper.deleteById(id);
    }



    @Override
    public PageResult<Activity> page(ActivityDTO activityDTO) {
        // 参数校验 无
        // 开始分页
        PageHelper.startPage(activityDTO.getPage(), activityDTO.getPageSize());
        // 查询
        List<Activity> list = activityMapper.list(activityDTO);
        // 获取信息
        Page<Activity> pageInfo = (Page<Activity>)list;
        // 返回结果
        return PageResult.<Activity>builder()
                .total(Integer.getInteger(pageInfo.getTotal()+""))
                .rows(pageInfo.getResult())
                .build();
    }
}
